package com.tongji.housediy.view.components
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.filters.GlowFilter;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	/**
	 * 
	 * @author chenchao
	 * 
	 */
	public class AbstractRulerField extends Sprite
	{
		protected var textField:TextField;
		protected var ratio:Number;
		protected var s1:Point, s2:Point, p1:Point, p2:Point;
		
		public function AbstractRulerField(p1X:Number, p1Y:Number, p2X:Number, p2Y:Number, k:Number, length:Number)
		{
			updatePointData(p1X, p1Y, p2X, p2Y, k, length);
			
			var textFormat:TextFormat = new TextFormat("黑体", 14);
			
			textField = new TextField();
			textField.filters = [new GlowFilter(0xFFFFFF, 1, 4, 4, 200)];
			textField.selectable = false;
			textField.autoSize = TextFieldAutoSize.CENTER;
			textField.defaultTextFormat = textFormat;
			textField.text = int(Point.distance(s1, s2)) + ' cm';
			
			this.addChild(textField);
		}
		
		protected function updatePointData(p1X:Number, p1Y:Number, p2X:Number, p2Y:Number, k:Number, length:Number):void
		{
			ratio = -1 / k;
			
			if (p1X != p2X)
			{
				if (p2Y > p1Y)
				{
					s1 = new Point(p1X + 8 / Math.sqrt(ratio * ratio + 1), p1Y + 8 * ratio / Math.sqrt(ratio * ratio + 1));
					s2 = new Point(p2X + 8 / Math.sqrt(ratio * ratio + 1), p2Y + 8 * ratio / Math.sqrt(ratio * ratio + 1));
					p1 = new Point(p1X + length / Math.sqrt(ratio * ratio + 1), p1Y + length * ratio / Math.sqrt(ratio * ratio + 1));
					p2 = new Point(p2X + length / Math.sqrt(ratio * ratio + 1), p2Y + length * ratio / Math.sqrt(ratio * ratio + 1));
				}
				else if (p2Y < p1Y)
				{
					s1 = new Point(p1X - 8 / Math.sqrt(ratio * ratio + 1), p1Y - 8 * ratio / Math.sqrt(ratio * ratio + 1));
					s2 = new Point(p2X - 8 / Math.sqrt(ratio * ratio + 1), p2Y - 8 * ratio / Math.sqrt(ratio * ratio + 1));
					p1 = new Point(p1X - length / Math.sqrt(ratio * ratio + 1), p1Y - length * ratio / Math.sqrt(ratio * ratio + 1));
					p2 = new Point(p2X - length / Math.sqrt(ratio * ratio + 1), p2Y - length * ratio / Math.sqrt(ratio * ratio + 1));
				}
				else
				{
					if (p1X < p2X)
					{
						s1 = new Point(p1X, p1Y - 8);
						s2 = new Point(p2X, p2Y - 8);
						p1 = new Point(p1X, p1Y - length);
						p2 = new Point(p2X, p2Y - length);
					}
					else
					{
						s1 = new Point(p1X, p1Y + 8);
						s2 = new Point(p2X, p2Y + 8);
						p1 = new Point(p1X, p1Y + length);
						p2 = new Point(p2X, p2Y + length);
					}
				}
			}
			else
			{
				if (p1Y < p2Y)
				{
					s1 = new Point(p1X + 8, p1Y);
					s2 = new Point(p2X + 8, p2Y);
					p1 = new Point(p1X + length, p1Y);
					p2 = new Point(p2X + length, p2Y);
				}
				else
				{
					s1 = new Point(p1X - 8, p1Y);
					s2 = new Point(p2X - 8, p2Y);
					p1 = new Point(p1X - length, p1Y);
					p2 = new Point(p2X - length, p2Y);
				}
			}
		}
	}
}